package com.example.tool.action;

import com.example.tool.ui.HToolIcon;
import com.example.tool.utils.FileUtil;
import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
 * <p>Title: MainMenuActions</p >
 * Description: 添加主菜单
 * <p>CreateTime: 2022/10/26</p>
 * @author FT
 */
public class MainMenuActions extends AnAction {

  @Override
  public @NotNull ActionUpdateThread getActionUpdateThread() {
    return super.getActionUpdateThread();
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    String[] configInformation = ActionUtil.getInformation(e.getProject());
    if (configInformation != null) {
      String basePath = "/" + configInformation[2] + "/WEB-INF/cfgHome/plugin/" + configInformation[1] + "/spring";
      String FileName = "spring-" + configInformation[1] + "-menu.xml";
      SwingUtil.setTips("仅生成主菜单的 <span style=\"color: #F56C6C;\">xml文件</span>，文件名称为 <span style=\"color: #F56C6C;\">" + FileName + "</span><br>创建的 <span style=\"color: #F56C6C;\">xml文件</span> 将会放在以下路径中。<br><span style=\"color: #F56C6C;\">" + basePath + "/</span>");
      ActionUtil.ActionCreatInput(e, (information, value) -> {

        // spring-xx-menu.xml
        String xmlPath = Objects.requireNonNull(e.getProject()).getBasePath() + basePath;
        String filePath = xmlPath + "/" + FileName;
        FileUtil.generateFile(filePath, "/demo/spring/spring-demo.txt",
            str -> ActionUtil.appendNewBean(str, "/demo/base/mainMenu.txt", filePath));
        VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
      });
      SwingUtil.setTips(null);
    } else {
      Messages.showMessageDialog(
          e.getProject(),
          "The seeyon configuration file is required, but the corresponding file is not found or the file is incorrect, please reset it!",
          "Error",
          HToolIcon.ModuleIcon);
    }
  }

  @Override
  public void update(@NotNull AnActionEvent event) {
    ActionUtil.ActionP(event);
  }
}